package com.tepth.remote.control.delegate.attendance;

import com.baidu.mapapi.model.LatLng;
import com.tepth.remote.control.base.config.ApiDictionary;
import com.tepth.remote.control.base.config.SpKey;
import com.tepth.remote.core.net.CommonParams;
import com.tepth.remote.core.net.callback.IError;
import com.tepth.remote.core.net.callback.ISoapSuccess;

import org.jetbrains.annotations.NotNull;
import org.ksoap2.serialization.SoapObject;

import java.util.ArrayList;
import java.util.WeakHashMap;

/**
 * Description:
 *
 * @author Hequn.Lee
 * @date 2018/11/21
 */
class AttendancePresenter extends AttendanceContract.AbstractPresenter {
    private final AttendanceContract.View mView;
    private final AttendanceContract.Model mModel;

    AttendancePresenter(@NotNull AttendanceContract.View view) {
        mView = view;
        mModel = new AttendanceModel();
    }

    @Override
    void getSubordinateNumber(String fencesId, final int type, final int fencesType, final int fencesCount) {
        mView.showLoading("正在加载");
        final WeakHashMap<String, Object> params = new WeakHashMap<>();
        params.put("eleId", fencesId);
        params.put("version", null);
        mModel.getFencesLatLngList(params,
                ApiDictionary.API_GET_FENCES,
                ApiDictionary.SOAP_NAME_SPACE,
                new ISoapSuccess() {
                    @Override
                    public void onSuccess(SoapObject object) {
                        mView.hideLoading();
                        final int code = Integer.parseInt(object.getProperty("Code").toString());
                        if (code == CommonParams.SUCCESS_CODE_NORMAL) {
                            final SoapObject value = (SoapObject) object
                                    .getProperty("Value");
                            mView.getFencesLatLngListSuccess(convertSoapToLatLng(value, type), fencesType, fencesCount);
                        } else {
                            mView.getFencesLatLngListFailed(code, "数据错误");
                        }
                    }
                }, new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        mView.getFencesLatLngListFailed(code, msg);
                    }
                });
    }

    private WeakHashMap<String, Object> convertSoapToLatLng(SoapObject response, int type) {
        WeakHashMap<String, Object> result = new WeakHashMap<>();
        final SoapObject value = (SoapObject) response.getProperty("LatLong");
        final int size = value.getPropertyCount();
        ArrayList<LatLng> latLngs = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            SoapObject object = (SoapObject) value.getProperty(i);
            if (object != null) {
                double lat = Double.parseDouble(object.getPrimitivePropertyAsString("EFL_Latitude_Center"));
                double lng = Double.parseDouble(object.getPrimitivePropertyAsString("EFL_Longitude_Center"));
                latLngs.add(new LatLng(lat, lng));
            }
        }
        if (type == 1) {
            final String radius = ((SoapObject) value.getProperty(size - 1)).getPrimitivePropertyAsString("EFL_Radius");
            result.put(SpKey.KEY_FENCES_RADIUS, (int) (Double.parseDouble(radius)));
        }
        result.put(SpKey.KEY_FENCES_LAT_LNG, latLngs);
        result.put(SpKey.KEY_FENCES_TYPE, type);
        return result;
    }
}
